<?php
namespace Modules\file\Controllers;

use Core\File;
use Core\Mvc\Controller;
use Modules\File\Models\File as FileModel;
use Core\Paginator\Adapter\Collection as CollectionPaginator;

/**
 */
class AdminController extends Controller
{

    public $user;

    public function indexAction($page)
    {
        $query = [
            'sort' => [
                'changed' => -1
            ],
            'limit' => 30,
            'skip' => 30 * ($page - 1)
        ];
        $extension = $this->config->get('extension');
        $params = $this->request->getQuery();
        if (isset($params['extension']) && isset($extension[$params['extension']])) {
            $type = $params['extension'];
        } else {
            $type = 'all';
        }
        $query = $this->_filterQuery($query);
        $results = FileModel::find($query);
        $dataPagination = new CollectionPaginator([
            'data' => $results,
            'query' => $query,
            'page' => $page,
            'collection' => '\Modules\File\Models\File',
        ]);

        self::$variables += array(
            'title' => strtoupper($type) . ' 列表',
            'description' => '第' . $page . '页',
            'breadcrumb' => array(
                'admin' => array(
                    'href' => array(
                        'for' => 'adminIndex',
                    ),
                    'name' => '控制台',
                ),
                'module' => array(
                    'name' => strtoupper($type) . ' 列表',
                ),
            ),
            'content' => array(),
        );
        $filterForm = $this->form->create($this->config->get('file.adminFilterForm'));
        $content['filter'] = array(
            '#templates' => 'box',
            'max' => false,
            'color' => 'widget',
            'hiddenTitle' => false,
            'size' => '12',
            'wrapper' => true,
            'content' => array(
                'filterForm' => $filterForm->renderForm(),
            ),
        );
        $content['ConfigList'] = array(
            '#templates' => 'box',
            'title' => '列表',
            'max' => false,
            'color' => 'primary',
            'size' => '12',
            'wrapper' => true,
            'content' => array(
                'dataList' => array(
                    '#templates' => array(
                        'file-manage',
                        'file-manage-' . $type,
                    ),
                    'data' => $dataPagination->getPaginate(),
                ),
            ),
        );
        self::$variables['content'] += $content;
    }

    protected function _filterQuery($query)
    {
        $params = $this->request->getQuery();
        foreach (array('state', 'access') as $value) {
            if (isset($params[$value])) {
                $query['conditions'][$value] = intval($params[$value]);
            }
        }
        if (isset($params['extension'])) {
            $contentType = $this->config->get('contentType');
            if (isset($contentType[$params['extension']])) {
                $query['conditions']['extension'] = $params['extension'];
            }
        }
        return $query;
    }

    public function deleteAction($id)
    {
        self::$variables['#templates'] = 'json';
        $file = FileModel::findById($id);
        if (!$file) {
            self::$variables['data'] = json_encode(array(
                'state' => false,
                'flash' => '文件不存在',
            ));
            return;
        }
        $fileModelDelte = $file->delete();
        $fileDelte = File::rm($file->getPath());
        if ($fileModelDelte && $fileDelte === true) {
            self::$variables['data'] = json_encode(array(
                'state' => true,
                'flash' => '文件删除成功',
            ));
            return;
        } else {
            $flash = '';
            if ($fileDelte !== true) {
                $flash .= $fileDelte . '<br />';
            }
            if (!$fileModelDelte) {
                foreach ($fileModelDelte->getMessages() as $message) {
                    $flash .= $message . "<br />";
                }
            }
            self::$variables['data'] = json_encode(array(
                'state' => false,
                'flash' => '文件删除文件失败：'.$flash,
            ));
        }
    }

}
